"use server";
import { Menu, Role } from "@prisma/client";
import { prisma } from "@/lib/db";
import { handlerAction } from "@/lib/handlerAction";
import dayjs from "dayjs";
import { cookies, headers } from "next/headers";

export const roleList = handlerAction<(Role & { menus: Menu[] })[]>(
  async () => {
    const list = await prisma.role.findMany({
      include: {
        userInfo: true,
      },
    });
    return list.map((it) => {
      return {
        ...it,
        createdAt: dayjs(it.createdAt).format("YYYY-MM-DD HH:mm:ss"),
      };
    });
  }
);
export const roleAdd = handlerAction<Role>(async (params: Role) => {
  const cookie = await cookies()
  const userId = cookie.get("userId")?.value;
  return prisma.role.create({
    data: params,
  });
});
export const roleDelete = handlerAction<Role>(async (params: Role) => {
  return prisma.role.delete({
    where: {
      id: params.id,
    },
  });
});
export const roleUpdate = handlerAction<Role>(async (params: Role) => {
  return prisma.role.update({
    where: {
      id: params.id,
    },
    data: params,
  });
});
